layui.define('excel', function (exports) {
    var $ = layui.$
        , excel = layui.excel
        , admin = layui.admin
        , view = layui.view,
        reExcel = {
            exportExcel: function (data, options) {
                options = options || {};

                // 生成配置的辅助函数，返回结果作为扩展功能的配置参数传入即可
                // 1. 复杂表头合并[B1,C1,D1][E1,F1]
                var mergeConf = excel.makeMergeConfig($.extend([], options.mergeConf));
                // 2. B列宽 150，F列宽200，默认80
                var colConf = excel.makeColConfig($.extend({}, options.cellWidth), options.cellDefaultWidth || 120);

                // 3. 第1行行高40，第二行行高30，默认20
                var rowConf = excel.makeRowConfig($.extend({}, options.rowHeight), options.rowDefaultHeight || 20);
                // 4. 公式的用法
                //data.push({
                //    id: '',
                //    username: '总年龄',
                //    age: { t: 'n', f: 'SUM(C4:C10)' },
                //    sex: '总分',
                //    score: { t: 'n', f: 'SUM(E4:E10)' },
                //    classify: {
                //        v: '注意：保护模式中公式无效，请「启用编辑」',
                //        s: { font: { color: { rgb: 'FF0000' } } }
                //    }
                //});

                if (options.sortField)
                    admin.jsonSort(data, options.sortField, true);

                if (!options.filterExportData) {
                    options.filterExportData = {};
                    $.each(options.cellTitle, function (k, v) {
                        options.filterExportData[k] = k;
                    });
                }

                data = excel.filterExportData(data, options.filterExportData);

                if (options.cellTitle)
                    data.unshift(options.cellTitle);

                //5. 样式
                excel.setExportCellStyle(data, options.titleRange || 'A1:' + String.fromCharCode(64 + Object.keys(options.cellTitle).length) + 1, $.extend({
                    s: {
                        alignment: {
                            horizontal: 'center', vertical: 'center'
                        }
                        , font: { sz: 12, bold: true }
                        , border: {
                            top: {
                                style: 'thin',
                                color: { indexed: 64 }
                            },
                            bottom: {
                                style: 'thin',
                                color: { indexed: 64 }
                            },
                            left: {
                                style: 'thin',
                                color: { indexed: 64 }
                            },
                            right: {
                                style: 'thin',
                                color: { indexed: 64 }
                            }
                        }
                    }
                }, options.titleStyle));

                if (data.length > 1)
                    excel.setExportCellStyle(data, options.cellRange || 'A2:' + String.fromCharCode(64 + Object.keys(options.cellTitle).length) + data.length, $.extend({
                        s: {
                            alignment: {
                                vertical: 'center'
                            }
                            , font: { sz: 11 }
                            , border: {
                                top: {
                                    style: 'thin',
                                    color: { indexed: 64 }
                                },
                                bottom: {
                                    style: 'thin',
                                    color: { indexed: 64 }
                                },
                                left: {
                                    style: 'thin',
                                    color: { indexed: 64 }
                                },
                                right: {
                                    style: 'thin',
                                    color: { indexed: 64 }
                                }
                            }
                        }
                    }, options.cellStyle));

                excel.exportExcel({
                    sheet1: data
                }, (options.fileName || '导出') + '_' + admin.getYMDHMS(new Date()) + '.xlsx', 'xlsx', {
                        extend: {
                            '!merges': mergeConf
                            , '!cols': colConf
                            , '!rows': rowConf
                        }
                    });
            }
            , uploadExcel: function (file, options) {
                try {
                    excel.importExcel(file, $.extend({
                        header: 0,
                        fields: options.cellTitle
                    }, options.importTitleConf), function (data) {
                        var sheetData = [];
                        $.each(data[0], function (idx, sheet) {
                            if (admin.hasArray(sheet) && sheet.length > 0) {
                                $.merge(sheetData, sheet);
                                if (options.mergeSheet === false) return false;
                            }
                        });

                        if (sheetData.length === 0)
                            return view.warn('没有可导入的数据。');

                        if (options.verifyData(sheetData) || false) {
                            view.warn('导入数据验证失败。');
                            return reExcel.exportExcel(sheetData, {
                                fileName: options.fileName
                                , cellWidth: options.cellWidth
                                , cellTitle: options.cellTitle
                                , filterExportData: options.verifyImportFilter
                            });
                        }

                            if (options.handleImportFilter) {
                                var handleImportFilter = {};
                                $.each(options.cellTitle, function (k, v) {
                                    handleImportFilter[k] = k;
                                });
                                sheetData = excel.filterExportData(sheetData, $.extend(handleImportFilter, options.handleImportFilter));
                            }
                            
                        admin.req({
                            url: options.url
                            , type: 'post'
                            , data: $.extend({ datas: JSON.stringify(sheetData) }, options.params ? options.params.call(this) : {} )
                            , loading: true
                            , tipOk:true
                            , success: options.success || null
                        });

                    });
                } catch (e) {
                    return view.error(e.message);
                }
            }
        };
    exports('reExcel', reExcel);
});